Using GitHub Action for CICD and Integrating Git Secrets
In the past two days, I migrated the CICD pipeline of a project from Jenkins to GitHub Action. I encountered some problems during the process and made a record.
First, here are some good tutorial links I found in these two days:
The first one is a tutorial written by the great Ruan Yifeng in 2019. It’s easy to understand. As expected of a master, he started writing tutorials when Action just came out.
Github Actions 入门教程The second is the official GitHub workflow syntax tutorial. It is recommended to read the table of contents thoroughly and then find the parts you need for detailed reading.
Workflow syntax for Github ActionsThe third is the official repo of git secrets. Git secrets is a tool that can be used to scan whether the code contains sensitive information such as passwords and usernames.
git secrets
Instructions:
- Read the instructions in combination with the yml file below and carefully check the comments in the yml file. I wrote a lot of information in the comments.
- Our environment is GitHub Enterprise Edition, with a self - hosted runner server, which is different from github.com.
- We adopted a relatively common way to trigger the CICD job: when a pull request is initiated on a branch, the CICD for the test environment will be triggered; when code is merged into the master branch, the CICD for the production environment will be triggered.
- I chose to place the actual running process of the pipeline in a container because in this way, all dependencies can be encapsulated into the container. Even if the runner server is replaced in the future, as long as Docker can be run, compilation can be carried out.
- Important!!! Since I configured the pattern of git secrets in the workflow configuration file (the yml file below) (that is, the
git secrets --add xxxxx
command), I need to configure git secrets not to detect this yml file (git secrets --add --allowed .github/workflows/main.yml:.*
). Otherwise, git secrets will always prompt that there is private information in the code, and the pipeline will fail. - Important!!! The configuration information of git secrets is in the .git directory of the repo. So remember to delete this repo every time before running. Otherwise, when executing
git secrets --scan
, it will fail.rm -rf *
cannot delete hidden directories, so you need to executerm -rf .git
separately. Points 4 and 5 cost me nearly a day… - I used the if syntax and branch name conditions to determine whether it should be deployed to the test environment or the production environment.
1 | name: CICD |